/**
 * 后台功能
 * */


(function () {
})();


//选项
function getOption(){
    $.ajax({
        type: apis.drugOption.method,
        url: apis.drugOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            var h = '', i;
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                return
            }
            cateAry = res.data.cate;
            h = '<option value="">-选择-</option>';
            for (i = 0; i < cateAry.length; i ++) {
                h += '<option value="' + cateAry[i].id + '">';
                h += cateAry[i].title;
                h += '</option>';
            }
            $('#cate').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取列表
function getList(){
    var formData = {
        start: pages.start, limit: pages.limit,
    };
    $.ajax({
        type: apis.drugIndex.method,
        url: apis.drugIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//HTML
function getHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].cate_name + '</td>';
        h += '<td>';
        if (d[i].img == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].img + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>' + d[i].zhuzhi + '</td>';
        h += '<td>' + d[i].period + '</td>';
        h += '<td>' + d[i].shelf_name + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_show(\'药品详情\',\'drug-show.html\', ' + d[i].id + ');">';
        h += '详情';
        h += '</a>';
        h += '&nbsp;';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'drug-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '</a>';
        h += '<br>';
        if (d[i].is_shelf == 0) {
            h += '<a href="javascript:;" onclick="shelf_yes(' + d[i].id + ');">';
            h += '上架';
            h += '</a>';
        } else {
            h += '<a href="javascript:;" onclick="shelf_no(' + d[i].id + ');">';
            h += '下架';
            h += '</a>';
        }
        h += '&nbsp;';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += '删除';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_tag(\'标签管理\',\'drug-tag.html\', ' + d[i].id + ');">';
        h += '标签修改';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_show(\'价格修改\',\'drug-price-edit.html\', ' + d[i].id + ');">';
        h += '价格修改';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_show(\'说明书修改\',\'drug-shu-edit.html\', ' + d[i].id + ');">';
        h += '说明书修改';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//修改弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//详情
function lay_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//标签列表
function lay_tag(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//详情
function showOne(v){
    if (!id) { return }
    $.ajax({
        type: apis.drugOne.method,
        url: apis.drugOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '';
            $('#title').html(d.title);
            $('#cate_name').html(d.cate_name);
            if (d.img != '') {
                h = '<img src="/' + d.img + '"/>';
                $('#img').html(h);
            }
            $('#price').html(d.price);
            $('#num').html(d.num);
            $('#symptom').html(d.symptom);
            $('#zhuzhi').html(d.zhuzhi);
            $('#yongfa').html(d.yongfa);
            $('#period').html(d.period);
            $('#instruction').html(d.instruction);
            $('#uptime').html(d.uptime);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取一个
function getOne(){
    if (!id) { return }
    $.ajax({
        type: apis.drugOne.method,
        url: apis.drugOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i;
            $('#title')[0].value = d.title;
            //分类
            console.log(cateAry);
            if (d.cate_id == 0) {
                h += '<option value="" selected>-选择-</option>';
            } else {
                h += '<option value="">-选择-</option>';
            }
            for (i = 0; i < cateAry.length; i ++) {
                if (cateAry[i].id == d.cate_id) {
                    h += '<option value="' + cateAry[i].id + '" selected>';
                } else {
                    h += '<option value="' + cateAry[i].id + '">';
                }
                h += cateAry[i].title;
                h += '</option>';
            }
            $('#cate').html(h);
            //图片
            if (d.img != '') {
                h = '<img src="/' + d.img + '"/>';
                h += '<input type="hidden" id="img" value="' + d.img + '"/>';
                $('#bigShow').html(h);
            }
            $('#price')[0].value = d.price;
            $('#num')[0].value = d.num;
            $('#symptom')[0].value = d.symptom;
            $('#zhuzhi')[0].value = d.zhuzhi;
            $('#yongfa')[0].value = d.yongfa;
            $('#period')[0].value = d.period;
            $('#instruction').html(d.instruction);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//修改
function modify(){
    var formData = {
        id: id,
        cate_id: $('#cate').val(),
        title: $('#title').val(),
        img: $('#img').val(),
        price: $('#price').val(),
        num: $('#num').val(),
        symptom: $('#symptom').val(),
        zhuzhi: $('#zhuzhi').val(),
        yongfa: $('#yongfa').val(),
        period: $('#period').val(),
        instruction: $('#instruction').val(),
    };
    if (!formData.cate_id) {
        msgPop({msg: '药品分类必须的'}); return
    }
    if (!formData.title) {
        msgPop({msg: '药品名称必须的'}); return
    }
    if (!formData.img) {
        msgPop({msg: '药品图片必须的'}); return
    }
    if (!formData.price) {
        msgPop({msg: '药品价格必须的'}); return
    }
    if (!formData.num) {
        msgPop({msg: '库存数量必须的'}); return
    }
    //console.log(formData); return
    $.ajax({
        type: apis.drugModify.method,
        url: apis.drugModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//上下架
function shelf_yes(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        shelf_api({id: v, shelf: 1});
    });
}
function shelf_no(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        shelf_api({id: v, shelf: 0});
    });
}
function shelf_api(e){
    $.ajax({
        type: apis.drugShelf.method,
        url: apis.drugShelf.url,
        data: { id: e.id, shelf: e.shelf },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            layerMsgOk('删除成功!');
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function lay_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.drugDel.method,
            url: apis.drugDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//分类列表
function cateList(){
    var formData = {
        start: pages.start, limit: pages.limit,
    };
    $.ajax({
        type: apis.drugCateIndex.method,
        url: apis.drugCateIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].uptime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="cate_edit(\'编辑\',\'drug-cate-edit.html\', ' + d[i].id + ');">';
                h += '编辑';
                h += '</a>';
                h += '<br>';
                h += '<a href="javascript:;" onclick="cate_del(' + d[i].id + ');" style="color: red;">';
                h += '删除';
                h += '</a>';
                h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
                h += '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//修改弹窗
function cate_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    tt = tt ? tt : '';
    url += '&title=' + escape(tt);
    layer_show(title,url,w,300);
}


//修改
function cateModify(){
    var formData = {
        id: id,
        title: $('#title').val(),
    };
    if (!formData.title) {
        msgPop({msg: '分类名称必须的'}); return
    }
    //console.log(formData); return
    $.ajax({
        type: apis.drugCateModify.method,
        url: apis.drugCateModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function cate_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.drugCateDel.method,
            url: apis.drugCateDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//标签列表
function tagList(){
    var formData = {
        drug_id: drug_id,
        start: pages.start, limit: pages.limit,
    };
    $.ajax({
        type: apis.drugTagIndex.method,
        url: apis.drugTagIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="tag_del(' + d[i].id + ');" style="color: red;">';
                h += '删除';
                h += '</a>';
                h += '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//修改
function tag_add(){
    var formData = {
        drug_id: drug_id,
        title: $('#title').val(),
    };
    if (!formData.title) {
        msgPop({msg: '标签名称必须的'}); return
    }
    //console.log(formData); return
    $.ajax({
        type: apis.drugTagModify.method,
        url: apis.drugTagModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function tag_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.drugTagDel.method,
            url: apis.drugTagDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    //top.location.reload();
                    location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}